﻿using System;
using Microsoft.Xna.Framework;

namespace Babylon.Toolbox
{
    public class Camera : CameraBase
    {
        Vector3 position;
        Vector3 target;
        Matrix view;
        
        public Vector3 Target
        {
            get { return target; }
            set
            {
                if (target == value)
                    return;

                target = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => Target);
            }
        }

        public override Vector3 Position
        {
            get { return position; }
            set
            {
                if (position == value)
                    return;

                position = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => Position);
            }
        }

        public float X
        {
            get { return position.X; }
            set
            {
                if (position.X == value)
                    return;

                position.X = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => X);
            }
        }

        public float Y
        {
            get { return position.Y; }
            set
            {
                if (position.Y == value)
                    return;

                position.Y = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => Y);
            }
        }

        public float Z
        {
            get { return position.Z; }
            set
            {
                if (position.Z == value)
                    return;

                position.Z = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => Z);
            }
        }

        public float TargetX
        {
            get { return target.X; }
            set
            {
                if (target.X == value)
                    return;

                target.X = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => TargetX);
            }
        }

        public float TargetY
        {
            get { return target.Y; }
            set
            {
                if (target.Y == value)
                    return;

                target.Y = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => TargetY);
            }
        }

        public float TargetZ
        {
            get { return target.Z; }
            set
            {
                if (target.Z == value)
                    return;

                target.Z = value;

                UpdateViewMatrix();
                RaisePropertyChanged(() => TargetZ);
            }
        }

        public Camera(string name, Guid id)
            : base(name, id)
        {
            position = new Vector3(0, 0, 1);
            Target = Vector3.Zero;
        }

        public Camera(string name = "Camera")
            : this(name, Guid.NewGuid())
        {
        }

        void UpdateViewMatrix()
        {
            view = Matrix.CreateLookAt(Position, Target, Vector3.Up);
        }

        public override Matrix View
        {
            get { return view; }
        }
    }
}
